package com.iplant.wms.serviceImpl.dao.wms;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.CalendarTool;
import com.iplant.base.utils.general.LockHelper;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.wms.WMSStockFreeze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class WMSStockFreezeDAO extends BaseDAO {

	private static WMSStockFreezeDAO Instance = null;
	private static Logger logger = LoggerFactory.getLogger(WMSStockFreezeDAO.class);
 

	private WMSStockFreezeDAO() {

		super();
		setTableName("wms_stockfreeze");
		mLockHelper = LockHelper.getLock(WMSStockFreezeDAO.class.getName());

	}

	public static WMSStockFreezeDAO getInstance() {
		if (Instance == null) {
			synchronized (WMSStockFreezeDAO.class) {
				if (Instance == null) {
					Instance = new WMSStockFreezeDAO();
				}
			}
		}
		return Instance;
	}

	public void WMS_UpdateStockFreeze(BMSEmployee wLoginUser, WMSStockFreeze wStockFreeze,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wStockFreeze == null || StringUtils.isEmpty(wStockFreeze.ProjectNo) || wStockFreeze.MaterialID <= 0
					|| wStockFreeze.StoreStationID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			WMSStockFreeze wStockFreezeO = this.WMS_CheckStockFreeze(wLoginUser, wStockFreeze, wErrorCode);
			if (wStockFreezeO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("StoreStationID", wStockFreeze.StoreStationID);
			wParwms.put("MaterialID", wStockFreeze.MaterialID);
			wParwms.put("MaterialNum", wStockFreeze.MaterialNum);

			if (StringUtils.isEmpty(wStockFreeze.Code))
				wStockFreeze.Code = super.CreateMaxCode(super.getTableName("wms_stockfreeze"),

						StringUtils.Format("KCDJ-{0}-", StringUtils.parseCalendarToString(Calendar.getInstance(), "yyMM")),
						"Code", 4,wStockFreeze,0);
			/**
			 * 自动生成
			 */
			wParwms.put("Code", wStockFreeze.Code);
			// 批号
			wParwms.put("BatchNo", wStockFreeze.BatchNo);
			wParwms.put("Description", wStockFreeze.Description);
			wParwms.put("ProjectNo", wStockFreeze.ProjectNo);

			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wStockFreeze.Active);

			if (wStockFreeze.ID > 0) {
				wParwms.put("ID", wStockFreeze.ID);
				super.Update(super.getTableName("wms_stockfreeze"), "ID", wParwms);
			} else {
				wParwms.put("CreatorID", wLoginUser.getID());
				wParwms.put("CreateTime", Calendar.getInstance());
				wStockFreeze.ID = super.Insert(super.getTableName("wms_stockfreeze"), wParwms);

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_UpdateStockFreeze", ex);
		}
	}

	public void WMS_ActiveStockFreeze(BMSEmployee wLoginUser, WMSStockFreeze wStockFreeze, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wStockFreeze == null || wStockFreeze.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("ID", wStockFreeze.ID);
			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wActive);
			super.Update(super.getTableName("wms_stockfreeze"), "ID", wParwms);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("WMS_ActiveStockFreeze", ex);
		}
	}

	public List<WMSStockFreeze> WMS_QueryStockFreezeList(BMSEmployee wLoginUser, int wFactoryID, int wStoreRegionID,
			int wStoreID, int wStoreAreaID, int wStoreShelfID, int wStoreStationID, int wMaterialID,
			String wMaterialCode, String wMaterialName, String wProjectNo, String wBatchNo, int wActive,
			Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.WMS_QueryStockFreezeList(wLoginUser, -1, "", wFactoryID, wStoreRegionID, wStoreID, wStoreAreaID,
				wStoreShelfID, wStoreStationID, wMaterialID, wMaterialCode, wMaterialName, wProjectNo, wBatchNo,
				wActive, wStartTime, wEndTime, wPagination, wErrorCode);
	}

	private List<WMSStockFreeze> WMS_QueryStockFreezeList(BMSEmployee wLoginUser, int wID, String wCode, int wFactoryID,
			int wStoreRegionID, int wStoreID, int wStoreAreaID, int wStoreShelfID, int wStoreStationID, int wMaterialID,
			String wMaterialCode, String wMaterialName, String wProjectNo, String wBatchNo, int wActive,
			Calendar wStartTime, Calendar wEndTime, Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<WMSStockFreeze> wWMSStockFreezeList = new ArrayList<WMSStockFreeze>();
		wErrorCode.set(0);

		try {
			Calendar wBaseTime = CalendarTool.getCalendar(2000, 1, 1);
			if (wStartTime.compareTo(wBaseTime) < 0)
				wStartTime = wBaseTime;
			if (wEndTime.compareTo(wBaseTime) < 0)
				wEndTime = wBaseTime;
			if (wStartTime.compareTo(wEndTime) > 0)
				return wWMSStockFreezeList;

			// Step02:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format("Select t.*,t0.Name as StoreStationName,"
					+ " t0.Code as StoreStationCode,t0.StoreAreaID,t0.StoreShelfID,"
					+ " t8.Name as StoreShelfName,t8.Code as StoreShelfCode,"
					+ " t7.Name as StoreAreaName,t7.Code as StoreAreaCode,t7.StoreID,"
					+ " t1.Name as StoreName,t1.Code as StoreCode,t1.FactoryID,"
					+ " t1.StoreRegionID,t5.Name as FactoryName,t5.Code as FactoryCode,"
					+ " t2.Name as StoreRegionName,t2.Code as StoreRegionCode,t6.MaterialName,"
					+ " t6.Code as MaterialCode,t6.CYUnitID as UnitID,t10.Name as UnitText,t10.Code as UnitCode, "
					+ " t3.Name as CreatorName ,t4.Name as EditorName  from {0}.wms_stockfreeze t  "
					+ " left join {0}.wms_storestation t0 on t.StoreStationID=t0.ID "
					+ " left join {0}.wms_store_shelf t8 on t0.StoreShelfID=t8.ID "
					+ " left join {0}.wms_storearea t7 on t0.StoreAreaID=t7.ID "
					+ " left join {0}.wms_store t1 on t7.StoreID=t1.ID "
					+ " left join {0}.fmc_factory t5 on t1.FactoryID=t5.ID "
					+ " left join {0}.wms_storeregion t2 on t1.StoreRegionID=t2.ID "
					+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
					+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
					+ " left join {0}.mss_material t6 on t.MaterialID=t6.ID "
					+ " left join {0}.mss_unit t10 on t6.CYUnitID=t10.ID "
					+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"

					+ " AND (:FactoryID<=0 or  t1.FactoryID=:FactoryID) "
					+ " AND (:StoreRegionID<=0 or  t1.StoreRegionID=:StoreRegionID) "
					+ " AND (:StoreID<=0 or  t7.StoreID=:StoreID) "
					+ " AND (:StoreAreaID<=0 or  t0.StoreAreaID=:StoreAreaID)"
					+ " AND (:StoreShelfID<=0 or  t0.StoreShelfID=:StoreShelfID)"
					+ " AND (:StoreStationID<=0 or  t.StoreStationID=:StoreStationID) AND (:MaterialID<=0 or  t.MaterialID=:MaterialID)"
					+ " AND (:Active<0 or  t.Active=:Active) and (:MaterialCode =''''  or  t6.Code=:MaterialCode)"
					+ " AND (:MaterialName = '''' or  t6.Name like :MaterialName) "
					+ " AND (:ProjectNo = '''' or  t.ProjectNo like :ProjectNo) "
					+ " AND (:BatchNo = '''' or  t.BatchNo like :BatchNo) "
					+ " AND ( :StartTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :StartTime <=  t.CreateTime )"
					+ " AND ( :EndTime <=str_to_date(''2010-01-01'', ''%Y-%m-%d'')  or :EndTime >=  t.CreateTime ) ",
					super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParwms.put("ID", wID);
			wParwms.put("Code", wCode);
			wParwms.put("FactoryID", wFactoryID);
			wParwms.put("StoreRegionID", wStoreRegionID);
			wParwms.put("StoreID", wStoreID);
			wParwms.put("StoreAreaID", wStoreAreaID);
			wParwms.put("StoreShelfID", wStoreShelfID);
			wParwms.put("StoreStationID", wStoreStationID);
			wParwms.put("MaterialID", wMaterialID);
			wParwms.put("MaterialCode", wMaterialCode);
			wParwms.put("MaterialName", StringUtils.isEmpty(wMaterialName) ? "" : ("%" + wMaterialName + "%"));
			wParwms.put("Active", wActive);
			wParwms.put("BatchNo", StringUtils.isEmpty(wBatchNo) ? "" : ("%" + wBatchNo + "%"));
			wParwms.put("ProjectNo", StringUtils.isEmpty(wProjectNo) ? "" : ("%" + wProjectNo + "%"));
			wParwms.put("StartTime", wStartTime);
			wParwms.put("EndTime", wEndTime);

			wWMSStockFreezeList = super.QueryForList(wSQLText, wParwms, wPagination, WMSStockFreeze.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_QueryStockFreezeList", ex);
		}
		return wWMSStockFreezeList;
	}

	public WMSStockFreeze WMS_CheckStockFreeze(BMSEmployee wLoginUser, WMSStockFreeze wWMSStockFreeze,
			OutResult<Integer> wErrorCode) {
		WMSStockFreeze wStockFreeze = new WMSStockFreeze();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format("Select * from {0}.wms_stockfreeze  where ID!=:ID "
					+ " and MaterialID=:MaterialID and StoreStationID=:StoreStationID "
					+ " and BatchNo=:BatchNo and ProjectNo=:ProjectNo and Active =1 ", super.getDBName());
			wParwms.clear();
			wParwms.put("ID", wWMSStockFreeze.ID);
			wParwms.put("MaterialID", wWMSStockFreeze.MaterialID);
			wParwms.put("StoreStationID", wWMSStockFreeze.StoreStationID);
			wParwms.put("BatchNo", wWMSStockFreeze.BatchNo); 
			wParwms.put("ProjectNo", wWMSStockFreeze.ProjectNo); 

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wStockFreeze.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wStockFreeze.MaterialID = StringUtils.parseInt(wSqlDataReader.get("MaterialID"));
				wStockFreeze.BatchNo = StringUtils.parseString(wSqlDataReader.get("BatchNo"));
				wStockFreeze.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

				wStockFreeze.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wStockFreeze.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wStockFreeze.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wStockFreeze.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wStockFreeze.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("WMS_CheckStockFreeze", ex);
		}
		return wStockFreeze;
	}

}
